package cn.stylefeng.guns.modular.tool.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.tool.entity.ToolBasicStopFiring;
import cn.stylefeng.guns.modular.tool.mapper.ToolBasicStopFiringMapper;
import cn.stylefeng.guns.modular.tool.model.params.ToolBasicStopFiringParam;
import cn.stylefeng.guns.modular.tool.model.result.ToolBasicStopFiringResult;
import  cn.stylefeng.guns.modular.tool.service.ToolBasicStopFiringService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * 停发地区设置 服务实现类
 */
@Service
public class ToolBasicStopFiringServiceImpl extends ServiceImpl<ToolBasicStopFiringMapper, ToolBasicStopFiring> implements ToolBasicStopFiringService {

    @Override
    public void add(ToolBasicStopFiringParam param){
        ToolBasicStopFiring entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ToolBasicStopFiringParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ToolBasicStopFiringParam param){
        ToolBasicStopFiring oldEntity = getOldEntity(param);
        ToolBasicStopFiring newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ToolBasicStopFiringResult findBySpec(ToolBasicStopFiringParam param){
        return null;
    }

    @Override
    public List<ToolBasicStopFiringResult> findListBySpec(ToolBasicStopFiringParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ToolBasicStopFiringParam param,String condition,Long operateId){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition, operateId);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ToolBasicStopFiringParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ToolBasicStopFiring getOldEntity(ToolBasicStopFiringParam param) {
        return this.getById(getKey(param));
    }

    private ToolBasicStopFiring getEntity(ToolBasicStopFiringParam param) {
        ToolBasicStopFiring entity = new ToolBasicStopFiring();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }
    
    @Resource
    private UserService userService;

	@Override
	@Transactional(rollbackFor = Exception.class,timeout = 100)
	public ResponseData getExceldata(MultipartFile file, ResponseData data) throws IOException {
		User user = userService.getcurrentUser();
		String fileName = file.getOriginalFilename();
		List<ToolBasicStopFiringParam> list = new ArrayList<ToolBasicStopFiringParam>();
		
		if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
			data.setMessage("上传文件格式不正确");
			data.setSuccess(false);
        }

		boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        Sheet sheet = wb.getSheetAt(0);
        
        for (int r = 1; r <= sheet.getLastRowNum()+1; r++) {
        	Row row = sheet.getRow(r);
            if (row == null){
                continue;
            }
            ToolBasicStopFiringParam toolBasicStopFiringParam = new ToolBasicStopFiringParam();

            String  province = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(0));
            if (ToolUtil.isNotEmpty(province)) {
            	toolBasicStopFiringParam.setProvince(province);
			} else {
				continue;
			}
            
            String  city = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(1));
            if (ToolUtil.isNotEmpty(city)) {
            	toolBasicStopFiringParam.setCity(city);
			} 
            
            String  areas = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(2));
            if (ToolUtil.isNotEmpty(areas)) {
            	toolBasicStopFiringParam.setAreas(areas);
			} 
            
            String  street = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(3));
            if (ToolUtil.isNotEmpty(street)) {
            	toolBasicStopFiringParam.setStreet(street);
			} 
            String address = toolBasicStopFiringParam.getProvince();
            if(ToolUtil.isNotEmpty(toolBasicStopFiringParam.getCity())) {
            	address += toolBasicStopFiringParam.getCity();
            	if(ToolUtil.isNotEmpty(toolBasicStopFiringParam.getAreas())) {
                	address += toolBasicStopFiringParam.getAreas();
                	if(ToolUtil.isNotEmpty(toolBasicStopFiringParam.getStreet())) {
                    	address += toolBasicStopFiringParam.getStreet();
                	}
            	}
            }
            toolBasicStopFiringParam.setAddress(address);

            toolBasicStopFiringParam.setStates("1");
            toolBasicStopFiringParam.setDeptId(user.getDeptId());
            list.add(toolBasicStopFiringParam);
        }
        if(list.size() > 0) {
        	this.saveBatchList(list);
        }
		return ResponseData.success();
	}

	@Override
	public boolean saveBatchList(List<ToolBasicStopFiringParam> savelist) {
		return this.baseMapper.saveBatchList(savelist);
	}

}
